package com.snail.billing.fragment;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.net.HttpApp;
import com.android.net.HttpResult;
import com.android.net.HttpSession;
import com.android.net.OnHttpResultListener;
import com.android.os.Page;
import com.android.util.AlertUtil;
import com.android.util.ResUtil;
import com.snail.billing.BillingBroadcast;
import com.snail.billing.BillingBroadcastImpl;
import com.snail.billing.DataCache;
import com.snail.billing.Resource;
import com.snail.billing.json.JsonEmailAuth;
import com.snail.billing.session.EmailAuthSession;
import com.snail.billing.util.BillingUtil;

public class EmailBindFragment extends Page implements View.OnClickListener, OnHttpResultListener {

	private TextView textTitle;
	private View buttonBack;

	private EditText inputEmail;
	private View button1;
	private EditText inputCaptcha;
	private View button2;

	private EmailAuthSession emailAuthSession;
	private String email;
	private String captcha;

	private boolean isUsed;

	@Override
	public int createLayoutId() {
		return ResUtil.getLayoutId(Resource.layout.snailbilling_email_bind_activity);
	}

	@Override
	public void onCreate() {
		super.onCreate();
		textTitle = (TextView) findViewById(ResUtil.getViewId(Resource.id.snailbilling_title_text));
		textTitle.setText(ResUtil.getString(Resource.string.snailbilling_bind_title_email));
		buttonBack = findViewById(ResUtil.getViewId(Resource.id.snailbilling_title_button_back));
		buttonBack.setOnClickListener(this);
		//
		inputEmail = (EditText) findViewById(ResUtil.getViewId(Resource.id.snailbilling_email_bind_input_email));
		button1 = findViewById(ResUtil.getViewId(Resource.id.snailbilling_email_bind_button1));
		button1.setOnClickListener(this);
		inputCaptcha = (EditText) findViewById(ResUtil.getViewId(Resource.id.snailbilling_email_bind_input_captcha));
		button2 = findViewById(ResUtil.getViewId(Resource.id.snailbilling_email_bind_button2));
		button2.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		if (v.equals(buttonBack)) {
			getPageManager().backward();
		} else if (v.equals(button1)) {
			isUsed = false;
			email = inputEmail.getText().toString();
			if (TextUtils.isEmpty((email))) {
				AlertUtil.show(getContext(), ResUtil.getString(Resource.string.snailbilling_bind_input_email));
				return;
			}
			if (!BillingUtil.isValidateEmail(email)) {
				AlertUtil.show(getContext(), ResUtil.getString(Resource.string.snailbilling_bind_input_email2));
				return;
			}
			requestEmailAuth("1", email, null);
		} else if (v.equals(button2)) {
			if (isUsed) {
				inputEmail.setEnabled(true);
				AlertUtil.show(getContext(), ResUtil.getString(Resource.string.snailbilling_bind_validate_captcha));
				return;
			} else {
				isUsed = true;
			}
			captcha = inputCaptcha.getText().toString();
			if (TextUtils.isEmpty(captcha)) {
				AlertUtil.show(getContext(), ResUtil.getString(Resource.string.snailbilling_bind_input_captcha));
				return;
			}
			requestEmailAuth("2", email, captcha);
		}
	}

	private void requestEmailAuth(String type, String email, String validateCode) {
		HttpApp httpApp = new HttpApp(getContext());
		emailAuthSession = new EmailAuthSession(type, email, validateCode);
		httpApp.setOnHttpResultListener(this);
		httpApp.request(emailAuthSession);
	}

	@Override
	public void onHttpResult(HttpResult httpResult) {
		if (httpResult.isSucceed()) {
			HttpSession httpSession = httpResult.getHttpSession();
			if (httpSession.equals(emailAuthSession)) {
				JsonEmailAuth JsonEmailAuth = new JsonEmailAuth((String) emailAuthSession.getResponseData());
				Toast.makeText(getContext(), JsonEmailAuth.getMessage(), Toast.LENGTH_SHORT).show();
				if (!TextUtils.isEmpty(JsonEmailAuth.getSessionId())) {
					DataCache.getInstance().importParams.sessionId = JsonEmailAuth.getSessionId();
				}
				if (JsonEmailAuth.getCode() == 1) {
					if (emailAuthSession.getType().equals("1")) {
						inputEmail.setEnabled(false);
					} else if (emailAuthSession.getType().equals("2")) {
						getPageManager().backward();
						String action = BillingBroadcast.ACTION_BIND_EMAIL_SUCCEED;
						BillingBroadcast broadcast = BillingBroadcastImpl.getInstance();
						broadcast.contains(action);
						if (broadcast.getResult() != null) {
							broadcast.getResult().onResult(1, action, new String[] { email });
						}
					}
				}
			}
		}
	}

}
